<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div class="header-title">
        <img src="../assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域  router是给侧边栏开启路由模式 详见element ui 文档-->
        <el-menu background-color="#EAEAEB" text-color="#000" active-text-color="#e9a135" 
                 :collapse="isCollapse" :collapse-transition="false" 
                 :default-active="activePath" unique-opened router>
          <!-- 一级菜单 -->
          <el-submenu :index="items.id + ''" v-for="items in menuList" :key="items.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 一级图标 -->
              <i :class="iconsObj[items.id]"></i>
              <span>{{items.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + list.path" v-for="list in items.children" 
                          :key="list.id" @click="saveNavState('/' + list.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 二级级图标 -->
                <i class="el-icon-menu"></i>
                <span>{{list.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name:'Home',
  data(){
    return {
      menuList:[],
      iconsObj:{
        '125':'el-icon-user',
        '103':'el-icon-key',
        '101':'el-icon-shopping-bag-2',
        '102':'el-icon-tickets',
        '145':'el-icon-pie-chart'
      },
      //是否折叠菜单栏
      isCollapse:false,
      //保存被激活的菜单栏
      activePath:''
    }
  },
  //生命周期函数created, Home组件被创建的同时就执行
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout(){
      //清空本地token
      window.sessionStorage.clear()
      //跳转到登录页
      this.$router.push('/login')
    },
    //获取所有的菜单
    async getMenuList(){
      const {data} = await this.$http.get('menus')
      // console.log(data);
      if(data.meta.status !== 200) return this.$message.error(data.meta.msg);
      this.menuList = data.data
    },
    //点击按钮切换菜单的折叠与展开
    toggleCollapse(){
      this.isCollapse = !this.isCollapse

    },
    //保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      //重新赋值
      this.activePath = activePath;
    }
  },
}
</script>

<style scoped>
  .el-container {
    height: 100%;
  }
  .el-header {
    background-color: #3D89C1;
    color: #fff;
    font-size: 25px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header-title {
    display: flex;
    align-items: center;
  }
  .header-title img {
    width: 60px;
    height: 60px;
    margin-right: 20px;
  }
  .el-aside {
    background-color: #EAEAEB;
    color: #333;
  }

  .el-main {
    background-color: #EEF1F7;
    color: #333;
  }
  .toggle-button {
    background-color: grey;
    line-height: 24px;
    font-size: 10px;
    text-align: center;
    color: #fff;
    letter-spacing: 0.5em;
    cursor: pointer;
  }
</style>